<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>13.罗马数组转整数</title>
</head>

<body>

</body>

<script>
    /**
     * @param {string} s
     * @return {number}
     */
    var romanToInt = function (s) {
        let sum = 0;
        let obj = {
            'I': 1,
            'V': 5,
            'X': 10,
            'L': 50,
            'C': 100,
            'D': 500,
            'M': 1000
        }
        let arr = s.split('');
        for (let i = 0; i < arr.length; i++) {
            if(obj[arr[i]]<obj[arr[i+1]]){
                sum -= obj[arr[i]];
            }else{
                sum += obj[arr[i]];
            }
        }
        return sum;
    };
    console.log(romanToInt('IV'));
</script>

</html>